﻿@typeparam T
@namespace Swimj.RazorLib.Partials
@inherits ComponentBase

@if (ToolbarContent != null)
{
    @ToolbarContent
}
else
{
    if (SearchPartial.IsAdd || SearchPartial.IsDeleteBatch || SearchPartial.IsImportExcel ||
        SearchPartial.IsImportHistory || SearchPartial.IsExportExcel || SearchPartial.IsExportHistory)
    {
        <RadzenRow>
            <RadzenColumn>
                @if (SearchPartial.IsAdd)
                {
                    <RadzenButton Icon="add_circle_outline" Text="Add" Variant="Variant.Outlined"
                                  Click=@(args => GridAddButtonClick(args)) @onclick:stopPropagation="true">
                        新增
                    </RadzenButton>
                }
                
                <RadzenButton Text="Add" Variant="Variant.Outlined"
                              Click=@(args => GridImportExcelButtonClick(args)) @onclick:stopPropagation="true">
                    导入
                </RadzenButton>
            </RadzenColumn>
        </RadzenRow>
    }
}

<RadzenDataGrid TItem="T" @ref="_requestTable" AllowRowSelectOnRowClick="@allowRowSelectOnRowClick"
                SelectionMode="DataGridSelectionMode.Multiple" @bind-Value=@_selectedItems AllowSorting="true"
                AllowPaging="true" PagerPosition="PagerPosition.Bottom" PagerHorizontalAlign="HorizontalAlign.Right"
                PageSize="10" PagerAlwaysVisible="true" PageSizeOptions="@pageSizeOptions"
                ShowPagingSummary="true" PagingSummaryFormat="@pagingSummaryFormat"
                IsLoading="@isLoading" Count="@Count" Data="@Data" LoadData="@LoadData"
                ColumnWidth="200px">
    <Columns>
        <RadzenDataGridColumn TItem="T" Width="60px" Sortable="false" Filterable="false">
            <HeaderTemplate>
                <RadzenCheckBox TriState="false" TValue="bool?"
                                Value="@(_selectedItems.Any() != true ? false : Data != null && !Data.All(i => _selectedItems.Contains(i)) ? null : Data != null && Data.Any(i => _selectedItems.Contains(i)))"
                                Change="@(args => { _selectedItems = args == true ? Data.ToList() : new List<T>();  })" />
            </HeaderTemplate>
            <Template Context="data">
                <RadzenCheckBox TriState="false" Value="@(_selectedItems.Contains(data))"
                                TValue="bool" Change=@(args => { if(!allowRowSelectOnRowClick) { _requestTable.SelectRow(data); }}) />
            </Template>
        </RadzenDataGridColumn>

        @CreateColumns()
        
    </Columns>
</RadzenDataGrid>

<CascadingValue Value="this" IsFixed>
    @ChildContent
</CascadingValue>

@code {

    private RadzenDataGrid<T> _requestTable = new();
    private IList<T> _selectedItems = new List<T>();

    private RenderFragment CreateColumns()
    {
        var type = typeof(T);
        var propertyInfos = type.GetProperties();

        return
    @<TemplatePartial>
        @foreach (var propertyInfo in propertyInfos)
        {
            var columnAttribute = propertyInfo.GetCustomAttribute<TableColumnAttribute>();
            if (columnAttribute != null)
            {
                if (columnAttribute.ColumnType == ColumnType.Text)
                {
                    <RadzenDataGridColumn TItem="T" Title="@columnAttribute.DisplayText" Type="propertyInfo.PropertyType"
                                          Property="@propertyInfo.Name">
                        <Template>
                            @propertyInfo.GetValue(context)
                        </Template>
                    </RadzenDataGridColumn>
                }
                else if (columnAttribute.ColumnType == ColumnType.Avatar)
                {
                    <RadzenDataGridColumn TItem="T" Title="@columnAttribute.DisplayText" Frozen="true" Sortable="false"
                                          Filterable="false" Width="80px" TextAlign="TextAlign.Center">
                        <Template>
                            <RadzenImage Path="@propertyInfo.GetValue(context)?.ToString()" class="rz-gravatar" />
                        </Template>
                    </RadzenDataGridColumn>
                }
                // else if (columnAttribute.ColumnType == ColumnType.Custom)
                // {
                //     var customColumn = Columns.FirstOrDefault(i => i.Field.Equals(propertyInfo.Name));
                //     if (customColumn != null && customColumn.ChildContent != null)
                //     {
                //         @customColumn.ChildContent(context)
                //     }
                // }
            }
        }

        @if (SearchPartial.IsEdit || SearchPartial.IsDetail || TemplateColumnContent != null)
        {
            <RadzenDataGridColumn TItem="T" Filterable="false" Sortable="false" Width="100px" TextAlign="TextAlign.Center">
                <Template Context="context">

                    @if (SearchPartial.IsEdit)
                    {
                        <RadzenButton ButtonStyle="ButtonStyle.Primary" Icon="edit" Size="ButtonSize.Medium"
                                      Shade="Shade.Lighter" Variant="Variant.Flat"
                                      Click=@(args => GridEditButtonClick(args, context)) @onclick:stopPropagation="true" />
                    }

                    @if (SearchPartial.IsDelete)
                    {
                        <RadzenButton ButtonStyle="ButtonStyle.Danger" Icon="delete" Size="ButtonSize.Medium"
                                      Shade="Shade.Lighter" Variant="Variant.Flat"
                                      Click=@(args => GridDeleteButtonClick(args, context)) @onclick:stopPropagation="true" />
                    }
                    @if (TemplateColumnContent != null)
                    {
                        @TemplateColumnContent(context)
                    }
                </Template>
            </RadzenDataGridColumn>
        }
    </TemplatePartial>;
    }

    public IList<T> GetSelectedItems()
    {
        return _selectedItems;
    }


}